package com.example.petinfo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.example.petinfo.dto.PetInfoRespDTO;
//import com.example.petinfo.dto.PetRegisterDTO;
//import com.example.petinfo.dto.PetUpdateDTO;
//import com.example.petinfo.service.PetInfoService;
//import com.example.user.context.UserContext;
import com.example.common.utils.UserContext;
import com.example.petinfo.domain.dto.PetInfoRespDTO;
import com.example.petinfo.domain.dto.PetRegisterDTO;
import com.example.petinfo.domain.dto.PetUpdateDTO;
import com.example.petinfo.domain.po.PetInfo;
import com.example.petinfo.service.PetInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.stream.Collectors;

/**
 * 宠物信息控制器（对外提供API接口）
 */
@RestController
@RequestMapping("/api/v1/pets")
@Api(tags = "宠物信息管理接口")
@RequiredArgsConstructor
public class PetInfoController {

    private final PetInfoService petInfoService;

    @PostMapping("/register")
    @ApiOperation("注册宠物（主人创建宠物档案）")
    public Long registerPet(@Validated @RequestBody PetRegisterDTO registerDTO) {
        // 从UserContext获取当前登录用户ID（主人ID）
        Long ownerUserId = UserContext.getUser();
        return petInfoService.registerPet(registerDTO, ownerUserId);
    }

    @PutMapping("/update")
    @ApiOperation("更新宠物信息（主人操作）")
    public Boolean updatePetInfo(@Validated @RequestBody PetUpdateDTO updateDTO) {
        Long operatorUserId = UserContext.getUser();
        return petInfoService.updatePetInfo(updateDTO, operatorUserId);
    }

    @GetMapping("/owner/list")
    @ApiOperation("分页查询主人的宠物列表（我的宠物）")
    public IPage<PetInfoRespDTO> getOwnerPetPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Long ownerUserId = UserContext.getUser();
        return petInfoService.getOwnerPetPage(ownerUserId, pageNum, pageSize);
    }

    @GetMapping("/{petId}")
    @ApiOperation("根据ID查询宠物详情")
    public PetInfoRespDTO getPetInfoById(@PathVariable Long petId) {
        return petInfoService.getPetInfoById(petId);
    }

    @GetMapping("/foster/search")
    @ApiOperation("代养人搜索可代养宠物（外借状态=1或6）")
    public IPage<PetInfoRespDTO> searchFosterPets(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String petCategory,
            @RequestParam(required = false) String petBreed) {
        return petInfoService.getPetByLendStatusPage(pageNum, pageSize, petCategory, petBreed);
    }

    /**
     * 统计指定状态的各品种宠物数量（供ranking-service调用）
     */
    @GetMapping("/api/v1/pets/breed-count")
    public Map<String, Integer> countBreedByLendStatus(
            @RequestParam Integer status1,
            @RequestParam Integer status2) {
        // 调用Mapper统计：按品种分组，计数状态为status1或status2的宠物
        return petInfoService.countBreedByLendStatus(status1, status2);
    }
}
